package com.shwread.address.tree_view;

import com.shwread.address.bean.FileBean;
import com.shwread.address.bean.Node;
import com.shwread.address.bean.QryEnterpriseBean;
import com.shwread.android.qysw10000227.R;

import java.util.ArrayList;
import java.util.List;

public class TreeHelper {
    /**
     * 传入我们的普通bean，转化为我们排序后的Node
     *
     * @param datas 数据
     * @param defaultExpandLevel 默认展开层级
     * @return List<Node>
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static <T> List<Node> getSortedNodes(List<T> datas, int defaultExpandLevel)
            throws IllegalArgumentException, IllegalAccessException {
        List<Node> result = new ArrayList<>();
        // 将用户数据转化为List<Node>
        List<Node> nodes = convetData2Node(datas);
        // 拿到根节点
        List<Node> rootNodes = getRootNodes(nodes);
        // 排序以及设置Node间关系
        for (Node node : rootNodes) {
            addNode(result, node, defaultExpandLevel, 1);
        }
        return result;
    }

    /**
     * 过滤出所有可见的Node
     */
    public static List<Node> filterVisibleNode(List<Node> nodes) {
        List<Node> result = new ArrayList<>();

        for (Node node : nodes) {
            // 如果为根节点，或者上层目录为展开状态
            if (node.isRoot() || node.isParentExpand()) {
                setNodeIcon(node);
                result.add(node);
            }
        }
        return result;
    }
    /**
     * 过滤出所有可见的Node
     */
    public static List<Node> VisibleNode(List<Node> nodes, int position) {
        Node node = nodes.get(position);
        List<Node> result = node.getChildren();
        if (node.isParentExpand()) {
            for (Node childrenNode : result) {
                setNodeIcon(childrenNode);
                nodes.add(++position, childrenNode);
            }
        } else {
            remove(result,nodes);
        }
        return nodes;
    }
    public static void remove(List<Node> result, List<Node> nodes) {
        for (Node childrenNode : result) {
            if (!childrenNode.isLeaf() && childrenNode.isExpand()) {
                remove(childrenNode.getChildren(),nodes);
            }
            nodes.remove(childrenNode);
        }
    }

    /**
     * 将我们的数据转化为树的节点
     *
     * @param datas datas
     * @return List<Node>
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    private static <T> List<Node> convetData2Node(List<T> datas)
            throws IllegalArgumentException, IllegalAccessException {
        List<Node> nodes = new ArrayList<>();
        Node node;
        for (T t : datas) {
//            QryEnterpriseBean bean = null;
//            Class<?> clazz = t.getClass();
//            Field[] declaredFields = clazz.getDeclaredFields();
//            for (Field f : declaredFields) {
//                if (f.getAnnotation(TreeNodeLabel.class) != null) {
//                    f.setAccessible(true);
//                    bean = (QryEnterpriseBean) f.get(t);
//                }
//                if (bean != null) {
//                    break;
//                }
//            }
//            node = new Node(id, pId, bean);
            QryEnterpriseBean bean = ((FileBean)t).getBean();
            node = new Node(bean);
            nodes.add(node);
        }

        // 设置Node间，父子关系;让每两个节点都比较一次，即可设置其中的关系
        for (int i = 0; i < nodes.size(); i++) {
            Node n = nodes.get(i);
            for (int j = i + 1; j < nodes.size(); j++) {
                Node m = nodes.get(j);
                if (m.getpId() == n.getId()) {
                    n.getChildren().add(m);
                    m.setParent(n);
                } else if (m.getId() == n.getpId()) {
                    m.getChildren().add(n);
                    n.setParent(m);
                }
            }
        }
        // 设置图片
        for (Node n : nodes) {
            setNodeIcon(n);
        }
        return nodes;
    }

    private static List<Node> getRootNodes(List<Node> nodes) {
        List<Node> root = new ArrayList<>();
        for (Node node : nodes) {
            if (node.isRoot())
                root.add(node);
        }
        return root;
    }

    /**
     * 把一个节点上的所有的内容都挂上去
     */
    private static void addNode(List<Node> nodes, Node node, int defaultExpandLeval, int currentLevel) {
        nodes.add(node);
        if (defaultExpandLeval >= currentLevel) {
            node.setExpand(true);
        }
        if (node.isLeaf()) {
            return;
        }
        for (int i = 0; i < node.getChildren().size(); i++) {
            addNode(nodes, node.getChildren().get(i), defaultExpandLeval, currentLevel + 1);
        }
    }

    /**
     * 设置节点的图标
     */
    private static void setNodeIcon(Node node) {
        if (node.getChildren().size() > 0 && node.isExpand()) {
            node.setIcon(R.drawable.change_store_icon_up);
        } else if (node.getChildren().size() > 0 && !node.isExpand()) {
            node.setIcon(R.drawable.change_store_icon);
        } else {
//            node.setIcon(-1);
            node.setIcon(R.drawable.more_right_bg);
        }
    }
}
